<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell title="我的频道" :border="false">
      <van-button
        class="edit-btn"
        size="small"
        color="red"
        round
        plain
        @click="isEdit = !isEdit"
        type="primary"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>

    <van-grid :border="false" class="my-grid" :gutter="10">
      <van-grid-item
        v-for="(item, index) in channels"
        :key="item.id"
        :class="{ active: index === active }"
        :text="item.name"
        @click="onMyClick(index,item.id)"
        :icon="isEdit && index !== 0 ? 'close' : ''"
      />
    </van-grid>

    <!-- 推荐频道 -->
    <van-cell title="推荐频道" :border="false" />

    <van-grid
      icon-size="0.3rem"
      direction="horizontal"
      :border="false"
      class="my-grid"
      :gutter="10"
    >
      <van-grid-item @click="onRecommend(item)" icon="plus" v-for="item in recommendChannels" :key="item.id" :text="item.name" />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannel } from '@/api/article.js'
import { setStorage } from '@/utils/storage.js'
import { addUserChannelItem, removeUserChannelItem } from '@/api/user.js'
import { Toast } from 'vant'
import { mapState } from 'vuex'
export default {
  props: {
    channels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      isEdit: false,
      allChannels: []
    }
  },

  created () {
    this.loadAllChannel()
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      return this.allChannels.filter(item => {
        return !this.channels.some(item2 => item2.id === item.id)
      })
    }
  },
  methods: {
    onMyClick (index, id) {
      if (this.isEdit) {
        if (index === 0) return
        if (index <= this.active) {
          this.$emit('setActive')
        }
        this.channels.splice(index, 1)
        this.deleteChannelStorage(id)
      } else {
        this.$emit('onGo', index)
      }
    },
    // 删除持久化
    async deleteChannelStorage (id) {
      if (!this.user) return setStorage('MY-CHANNELS', this.channels)
      Toast.loading({
        message: '删除中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await removeUserChannelItem(id)
        Toast.success('删除成功')
      } catch (err) {
        Toast.fail('删除失败')
      }
    },
    async loadAllChannel () {
      try {
        const res = await getAllChannel()
        this.allChannels = res.channels
      } catch (err) {
        Toast.fail('获取所有频道列表失败')
      }
    },
    async onRecommend (obj) {
      this.channels.push(obj)

      Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })

      // 数据持久化
      if (this.user) {
        // 登陆了
        try {
          await addUserChannelItem({ id: obj.id, seq: this.channels.length })
          Toast.success('获取数据成功')
        } catch (err) {
          Toast.fail('获取数据失败')
        }
      } else {
        // 未登录
        setStorage('MY-CHANNELS', this.channels)
        Toast.success('获取数据成功')
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  .edit-btn {
    width: 104px;
    height: 48px;
  }

  /deep/.my-grid {
    .van-grid-item {
      width: 160px;
      height: 86px;

      border-radius: 6px;
    }
    .van-grid-item__content {
      background: #f4f5f6;
    }
    .van-icon-close {
      font-size: 35px;
      position: absolute;
      top: -10.5px;
      right: 5px;
    }
    .van-grid-item__icon + .van-grid-item__text {
      margin-top: 0;
    }
    .active {
      .van-grid-item__text {
        color: red;
      }
    }
  }
}
</style>
